package com.stormrage.edu.serv.adm.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.stormrage.edu.dao.adm.AdmResDao;
import com.stormrage.edu.serv.adm.AdmResServ;
import com.stormrage.entity.adm.AdmRes;
import com.stormrage.entity.adm.rep.AdmResRep;
import com.stormragetech.core.exception.StormrageTechException;

/**
 * @author www.stormragetech.com
 * @date 2016年1月19日
 * @version 1.0.0
 */
@Service
public class AdmResServImpl implements AdmResServ {

	private static final Logger logger = LoggerFactory.getLogger(AdmResServImpl.class);

	@Autowired
	private AdmResDao resDao;

	@Override
	public Integer queryCountByAcctId(final Integer arg0) throws StormrageTechException {

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("acctId", arg0);

		Integer count = resDao.queryCountByAcctId(map);
		logger.debug(" - 查询结果为 -" + count);
		return count;
	}

	@Override
	public AdmRes queryById(final Integer arg0) throws StormrageTechException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);
		AdmRes entity = resDao.queryById(map);
		logger.debug(" - 查询结果为 -" + entity);
		return entity;
	}

	@Override
	public List<AdmRes> queryList() throws StormrageTechException {

		List<AdmRes> array = resDao.queryList();

		logger.debug(" - 查询结果为 -" + array);
		return array;

	}

	@Override
	public List<AdmResRep> queryList(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 AdmResServ 】- 【 queryList 】-【 STARTED 】");

		List<AdmRes> array = resDao.queryList();

		List<AdmResRep> list = new ArrayList<AdmResRep>();

		List<AdmRes> resList = queryListByAcctId(arg0);

		Iterator<AdmRes> it = array.iterator();
		while (it.hasNext()) {
			AdmRes res = it.next();
			if (null != arg0) {

				logger.debug(" 是否包含【" + res.getId() + "】-【" + res.getRes() + "】 权限 " + resList.contains(res));

				AdmResRep rep = null;
				if (resList.contains(res))
					rep = AdmResRep.instance(res, true);
				else
					rep = AdmResRep.instance(res, false);

				list.add(rep);

			} else {
				AdmResRep rep = AdmResRep.instance(res, false);
				list.add(rep);
			}
		}

		logger.debug(" - 查询结果为 -" + list);

		logger.debug(" 【 Serv 】- 【 AdmResServ 】- 【 queryList 】-【 COMPLETED 】");
		return list;

	}

	@Override
	public List<AdmRes> queryListByTypeParent(final String arg0, final Integer arg1) throws StormrageTechException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("resType", arg0);
		map.put("parentId", arg1);
		List<AdmRes> array = resDao.queryListByTypeParent(map);
		logger.debug(" - 查询结果为 -" + array);
		return array;
	}

	@Override
	public List<AdmRes> queryListByAcctId(final Integer arg0) throws StormrageTechException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("acctId", arg0);
		List<AdmRes> array = resDao.queryListByAcctId(map);
		logger.debug(" - 查询结果为 -" + array);
		return array;
	}

}
